package arrAndstr;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/*
    128. 最长连续序列
 */
public class T128 {
    public static void main(String[] args) {
        int[] nums = new int[]{100,4,200,1,3,2};
        System.out.println(longestConsecutive(nums));
    }

    public static int longestConsecutive(int[] nums) {
        int len = nums.length;
        if (len==0 || len==1) return len;
        // 去重、排序 --> set
        Set<Integer> set = new TreeSet<>();
        for (int num : nums) {
            set.add(num);
        }

        int result = 1;
        int beforeNum = 0;
        for (Integer num : set) {
            beforeNum = num;
            break;
        }

        int tempResult = result;
        for (Integer num : set) {
            if (num != beforeNum) {
                if (num == beforeNum+1) {
                    tempResult++;
                } else {
                    tempResult = 1;
                }
                beforeNum = num;
            }
            result = result < tempResult ? tempResult :result;
        }
        return result;
    }
}
